package com.example.wuyeguanli.tools;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Complaint;
import java.util.List;

public class ComplaintAdapter extends RecyclerView.Adapter<ComplaintAdapter.ComplaintViewHolder> {

    private List<Complaint> complaintList;
    private Context context;

    public ComplaintAdapter(List<Complaint> complaintList, Context context) {
        this.complaintList = complaintList;
        this.context = context;
    }

    @NonNull
    @Override
    public ComplaintViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.complaint_windows, parent, false);
        return new ComplaintViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ComplaintViewHolder holder, int position) {
        Complaint complaint = complaintList.get(position);
        holder.workOrderId.setText("工单号: #" + complaint.getWorkOrderId());
        holder.message.setText(complaint.getMessage());

        // 根据图片ID加载图片
        if (complaint.getImageId() != null && !complaint.getImageId().isEmpty()) {
            holder.imageIndicator.setVisibility(View.VISIBLE);
            // 拼接完整的图片URL，注意要用你的服务器基础URL
            String imageUrl = ApiClient.BASE_URL + "/image/" + complaint.getImageId();
            GlideApp.with(context)
                    .load(imageUrl)
                    .placeholder(R.drawable.ic_loading_placeholder) // 加载中的占位图
                    .error(R.drawable.ic_load_error_placeholder)      // 加载失败的占位图
                    .into(holder.imageIndicator);
        } else {
            holder.imageIndicator.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return complaintList.size();
    }

    public static class ComplaintViewHolder extends RecyclerView.ViewHolder {
        TextView workOrderId;
        TextView message;
        ImageView imageIndicator;

        public ComplaintViewHolder(@NonNull View itemView) {
            super(itemView);
            workOrderId = itemView.findViewById(R.id.complaint_work_order_id);
            message = itemView.findViewById(R.id.complaint_message);
            imageIndicator = itemView.findViewById(R.id.complaint_image_indicator);
        }
    }
}